#ifndef __TRANSMIT_H__
#define __TRANSMIT_H__

#include "includes.h"
#include "drivers.h"
#include "vehicle.h"
#include "gps.h"

#define NAME_MAX_SIZE       32

#define SERVER_IS_K         1

#ifdef SERVER_IS_K
    #define KEY_DEVICE_ID       "deviceid"
    #define KEY_MSG_TYPE        "msg_type"
    #define KEY_HEARTBEAT       "heartbeat_count"
    #define KEY_CMD_ID          "cmd_id"
    #define KEY_VEHICLE_TYPE    "model"
    #define KEY_STATUS          "status"
    #define KEY_CMD_TYPE        "cmd_type"
    #define KEY_LNG             "lng"
    #define KEY_LAT             "lat"
#elif defined SERVER_IS_VEHICLE_UNION
    #define KEY_DEVICE_ID       "deviceid"
    #define KEY_MSG_TYPE        "msg_type"
    #define KEY_HEARTBEAT       "heartbeat_count"
    #define KEY_CMD_ID          "cmd_id"
    #define KEY_VEHICLE_TYPE    "model"
    #define KEY_STATUS          "status"
    #define KEY_CMD_TYPE        "cmd_type"
    #define KEY_LNG             "lng"
    #define KEY_LAT             "lat"
#endif

#define KEY_TIME                "time"
#define KEY_LOGIN_TIME          "time"
#define KEY_LOCATION_COUNT      "count"

#define LOGIN_DELAYED_TIME          6

#ifdef SERVER_IS_K
    #define MSG_TYPE_HEARTBEAT          0
    #define MSG_TYPE_HEARTBEAT_RSP      1
    #define MSG_TYPE_CTRL               2
    #define MSG_TYPE_CTRL_RSP           3
    #define MSG_TYPE_UPLOAD             4
    #define MSG_TYPE_FAULT_CODE         5
    #define MSG_TYPE_CLEAR_FAULT        6
    #define MSG_TYPE_LOCATION           7
    #define MSG_TYPE_LOGIN              8
    #define MSG_TYPE_LOGIN_RSP          9
    #define MSG_TYPE_VEHICLE_TYPE       10
    #define MSG_TYPE_RETRY              11
#elif defined SERVER_IS_VEHICLE_UNION
    #define MSG_TYPE_HEARTBEAT          2
    #define MSG_TYPE_HEARTBEAT_RSP      2
    #define MSG_TYPE_CTRL               3
    #define MSG_TYPE_CTRL_RSP           4
    #define MSG_TYPE_UPLOAD             0
    #define MSG_TYPE_FAULT_CODE         5
    #define MSG_TYPE_CLEAR_FAULT        6
    /*           UNUSED BELOW          */
    /*                 |               */
    /*                 |               */
    /*                 v               */
    #define MSG_TYPE_LOCATION           7
    #define MSG_TYPE_LOGIN              8
    #define MSG_TYPE_LOGIN_RSP          9
    #define MSG_TYPE_VEHICLE_TYPE       10
    #define MSG_TYPE_RETRY              11
    /*                 ^               */
    /*                 |               */
    /*                 |               */
    /*                                 */
#endif

#define MSG_TYPE_CHECK_EPO              12
#define MSG_TYPE_CHECK_EPO_RSP          13
#define MSG_TYPE_FRAGMENT_RSP           15
#define MSG_TYPE_MULTI_LOCATION         16

typedef struct {
    uint8_t id;
    uint32_t cmd_id;
    uint8_t value;
} CtrlMsg;

typedef struct {
    uint8_t id;
    char key[NAME_MAX_SIZE];
} CtrlItem;

typedef struct {
    uint8_t index;
    char key[NAME_MAX_SIZE];
} PidItem;

typedef struct {
    uint8_t heartbeat;
    OS_MUTEX mutex;
    uint8_t (*send)(uint8_t *buf, uint16_t len);
} Transmission;

void transmit_init(void);

void transmit_check_epo(void);
void transmit_login(void);
void transmit_heartbeat(uint8_t count);
void transmit_control_rsp(uint32_t cmd_id, uint8_t id);
void transmit_location(Location *location);
void transmit_vin_code(VinCode *code);
uint8_t transmit_eng_data(EngineDataMap *engData);
uint8_t transmit_bcm_data(BcmDataMap *bcmData);
uint8_t transmit_ipc_data(IpcDataMap *ipcData);
void transmit_lock(void);
void transmit_unlock(void);
char* transmit_get_key(uint8_t i);
char* transmit_get_bcm_key(uint8_t i);
char* transmit_get_ipc_key(uint8_t i);
void transmit_codes_data(FaultCodeItem *item);
char *transmit_get_deviceid(void);
void transmit_update_deviceid(void);
void transmit_multi_location(nmea_msg *msg, uint8_t count);

static void transmit_process_task(void *parg);
static void transmit_heartbeat_task(void *parg);
static void transmit_upload_task(void *parg);

extern OS_FLAG_GRP FLAG_TaskRunStatus;
#endif
